<template>
    <div class="container-content">
        <div class="container-content-title">
            <div>{{ $t('platform.setting.access-key.title') }} (Api) <span v-if="config.payment != 1">(Wallet)</span></div>
            <div class="flex" v-if="dataStatus == 'dle' && data.id != null">
                <el-button type="text" class="p-0" @click="dialogRestore" v-if="data.deleted != null">{{ $t('action.on') }}</el-button>
                <el-button type="text" class="p-0" @click="dialogDestroy" v-if="data.deleted == null">{{ $t('action.off') }}</el-button>
                <el-button type="text" class="p-0" @click="dialogUpdate" v-if="data.deleted == null">{{ $t('action.update') }}</el-button>
            </div>
        </div>
        <component-page-loading :status="dataStatus" v-if="['loading', 'error'].indexOf(dataStatus) >= 0" @reload="getData"></component-page-loading>
        <component-page-loading status="nodata" nodata-init @create="dialogCreate" v-else-if="data.id == null"></component-page-loading>
        <el-form label-width="180px" class="detail" v-else>
            <el-form-item label="AccessKeyId">
                <div :class="{'line-through': data.deleted != null}">{{ data.access_key_id }}</div>
            </el-form-item>
            <el-form-item label="AccessKeySecret">
                <div :class="{'line-through': data.deleted != null}">{{ data.access_key_secret }}</div>
            </el-form-item>
        </el-form>
        <el-dialog :title="$t('platform.setting.access-key.dialog.create')" center width="320px" :visible.sync="dialog.visible.create">
            <div class="text-center" v-html="$t('confirm.access-key', { status: $t('action.create') })"></div>
            <div slot="footer">
                <el-button type="primary" @click="updateOrCreate">{{ $t('action.confirm') }}</el-button>
                <el-button @click="dialog.visible.create = false">{{ $t('action.cancel') }}</el-button>
            </div>
        </el-dialog>
        <el-dialog :title="$t('platform.setting.access-key.dialog.update')" center width="320px" :visible.sync="dialog.visible.update">
            <div class="text-center" v-html="$t('confirm.access-key', { status: $t('action.update') })"></div>
            <div slot="footer">
                <el-button type="primary" @click="updateOrCreate">{{ $t('action.confirm') }}</el-button>
                <el-button @click="dialog.visible.update = false">{{ $t('action.cancel') }}</el-button>
            </div>
        </el-dialog>
        <el-dialog :title="$t('platform.setting.access-key.dialog.destroy')" center width="320px" :visible.sync="dialog.visible.destroy">
            <div class="text-center" v-html="$t('confirm.access-key', { status: $t('action.off') })"></div>
            <div slot="footer">
                <el-button type="primary" @click="destroy">{{ $t('action.confirm') }}</el-button>
                <el-button @click="dialog.visible.destroy = false">{{ $t('action.cancel') }}</el-button>
            </div>
        </el-dialog>
        <el-dialog :title="$t('platform.setting.access-key.dialog.restore')" center width="320px" :visible.sync="dialog.visible.restore">
            <div class="text-center" v-html="$t('confirm.access-key', { status: $t('action.on') })"></div>
            <div slot="footer">
                <el-button type="primary" @click="restore">{{ $t('action.confirm') }}</el-button>
                <el-button @click="dialog.visible.restore = false">{{ $t('action.cancel') }}</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
export default {
    name: "PlatformAccessKey",
    props: ['tree', 'config'],
    data() {
        return {
            dataStatus: 'dle',
            data: {
                access_key_id: null,
                access_key_secret: null,
            },
            dataInit: {},
            dialog: {
                visible: {
                    create: false,
                    update: false,
                    destroy: false,
                    restore: false,
                }
            },
        }
    },
    watch: {
        tree: {
            deep: true,
            immediate: true,
            handler(n, o) {
                // 获取数据
                n.id != null && this.getData();
            }
        },
    },
    created() {
        // 备份初始数据
        this.dataInit = _.cloneDeep(this.data);
    },
    methods: {
        // 获取数据
        getData() {
            this.dataStatus = 'loading';
            axios.get('/platform/setting/access-key/getData', {
                params: {
                    plat_code: this.tree.code
                }
            }).then((response) => {
                if (_.includes([200, 44100], response.data.resp_msg.code)) {
                    this.data = response.data.resp_data || _.cloneDeep(this.dataInit);
                    this.dataStatus = 'dle';
                    // 广播事件到父组件
                    this.$emit('update', this.data);
                } else {
                    this.dataStatus = 'error';
                }
            })
        },
        // 显示模态框（创建）
        dialogCreate() {
            // 显示模态框
            this.dialog.visible.create = true;
        },
        // 显示模态框（编辑）
        dialogUpdate() {
            // 显示模态框
            this.dialog.visible.update = true;
        },
        // 显示模态框（禁用）
        dialogDestroy() {
            // 显示模态框
            this.dialog.visible.destroy = true;
        },
        // 显示模态框（恢复）
        dialogRestore() {
            // 显示模态框
            this.dialog.visible.restore = true;
        },
        updateOrCreate() {
            // loading 状态 start
            let loading = this.$loading();
            // 保存数据
            axios.post('/platform/setting/access-key/updateOrCreate', {
                plat_code: this.tree.code
            }).then((response) => {
                // loading 状态 close
                loading.close();
                // 逻辑处理
                if (response.data.resp_msg.code == 200) {
                    this.$message({
                        type: 'success',
                        message: this.$t('messages.succeeded', {status: this.data.id == null ? this.$t('action.create') : this.$t('action.update')}),
                        showClose: true
                    });
                    // 更新数据
                    this.data = response.data.resp_data;
                    // 广播事件到父组件
                    this.$emit('update', this.data);
                    // 关闭模态框
                    this.dialog.visible.create = false;
                    this.dialog.visible.update = false;
                } else {
                    this.$message({
                        type: 'error',
                        dangerouslyUseHTMLString: true,
                        message: this.$t('messages.failed', {status: this.data.id == null ? this.$t('action.create') : this.$t('action.update')})
                            + `<div class="mt-2">${response.data.resp_msg.message}</div>`,
                        showClose: true
                    });
                }
            })
        },
        destroy() {
            // loading 状态 start
            let loading = this.$loading();
            // 保存数据
            axios.post('/platform/setting/access-key/destroy', {
                plat_code: this.tree.code
            }).then((response) => {
                // loading 状态 close
                loading.close();
                // 逻辑处理
                if (response.data.resp_msg.code == 200) {
                    this.$message({
                        type: 'success',
                        message: this.$t('messages.succeeded', {status: this.$t('action.off')}),
                        showClose: true
                    });
                    // 更新数据
                    this.data = response.data.resp_data;
                    // 广播事件到父组件
                    this.$emit('update', this.data);
                    // 关闭模态框
                    this.dialog.visible.destroy = false;
                } else {
                    this.$message({
                        type: 'error',
                        dangerouslyUseHTMLString: true,
                        message: this.$t('messages.failed', {status: this.$t('action.off')}) + `<div class="mt-2">${response.data.resp_msg.message}</div>`,
                        showClose: true
                    });
                }
            })
        },
        restore() {
            // loading 状态 start
            let loading = this.$loading();
            // 保存数据
            axios.post('/platform/setting/access-key/restore', {
                plat_code: this.tree.code
            }).then((response) => {
                // loading 状态 close
                loading.close();
                // 逻辑处理
                if (response.data.resp_msg.code == 200) {
                    this.$message({
                        type: 'success',
                        message: this.$t('messages.succeeded', {status: this.$t('action.on')}),
                        showClose: true
                    });
                    // 更新数据
                    this.data = response.data.resp_data;
                    // 广播事件到父组件
                    this.$emit('update', this.data);
                    // 关闭模态框
                    this.dialog.visible.restore = false;
                } else {
                    this.$message({
                        type: 'error',
                        dangerouslyUseHTMLString: true,
                        message: this.$t('messages.failed', {status: this.$t('action.on')}) + `<div class="mt-2">${response.data.resp_msg.message}</div>`,
                        showClose: true
                    });
                }
            })
        },
    }
}
</script>

<style scoped>

</style>
